package com.ermao.utils;

import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 用来进行校验一系列来自前端输入的工具
 * @author Ermao
 * Date: 2021/9/3 12:24
 */
public class CheckValidUtils {

	public static boolean checkEmail(String email) {
		if (email == null || email.equals("")){
			return false;
		}
		String regEx = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
		Pattern p = Pattern.compile(regEx);
		Matcher m = p.matcher(email);
		return m.matches();
	}

	public static boolean checkAccountNumber(Integer accountNumber) {
		if (accountNumber == null) {
			return false;
		}
		return (accountNumber > 9999_9999 && accountNumber < 10_0000_0000);
	}

	public static boolean checkAccountNumber(int accountNumber) {
		return (accountNumber > 9999_9999 && accountNumber < 10_0000_0000);
	}

	public static boolean checkAccountNumber(String StrAccountNumber) {
		if (StrAccountNumber == null || StrAccountNumber.equals("")) {
			return false;
		}
		int accountNumber = Integer.parseInt(StrAccountNumber);
		return (accountNumber > 9999_9999 && accountNumber < 10_0000_0000);
	}

	public static boolean checkNickname(String nickname) {
		return nickname.length() <= 10;
	}

	public static boolean checkPassword(String password) {
		if (password == null || password.equals("")) {
			return false;
		}
		String regex = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$";
		return password.matches(regex);
	}

	public static boolean checkUser(Integer accountNumber, String password) {
		return (checkAccountNumber(accountNumber) && checkPassword(password));
	}

	public static boolean CheckAccountValid(Double accountAmount, Integer accountType, Date accountTime, String accountComment) {
		if (accountAmount == null || accountAmount <= 0) {
			return false;
		}
		if (accountType == null || accountType < 0 || accountType > 1) {
			return false;
		}
		if (accountTime == null) {
			return false;
		}
		if (accountComment == null || accountComment.length() > 4) {
			return false;
		}
		return true;
	}

	public static boolean checkCategoryNameValid(String categoryName) {
		return categoryName != null && !categoryName.equals("") && categoryName.length() <= 4;
	}

	public static boolean checkCategoryTypeValid(Integer type) {
		return type != null && type >= 0 && type <= 1;
	}

	public static boolean checkFromAndEndTimeValid(Date fromTime, Date endTime) {
		return !fromTime.after(endTime);
	}
}
